


world.onPlayerPurchaseSuccess(({ userId, productId, orderId }) => {
    let entity = world.querySelectorAll('player').filter(e => e.player.userId === userId)[0];
    if (entity) {
        if (productId === 383039918126154) {
            entity.data.bk += 1;
            entity.player.directMessage(`购买成功！获得“智能芯片”x1`);

        }
        if (productId === 383034100626341) {
            entity.data.bk+=6
            entity.player.directMessage(`购买成功！获得“智能芯片”x6`);

        }
        if (productId === 383049049126122) {
            entity.data.bk+=24
            entity.player.directMessage(`购买成功！获得“智能芯片”x24`);

        }
        if (productId === 383037552539055) {
            entity.gainItem('telc', false)
            entity.gainItem('telc', false)
            entity.gainItem('telc', false)
            entity.gainItem('telc', false)
            entity.gainItem('telc', false)
        }
        if (productId === 383034096431908) {
            entity.gainItem('gunG', false)

        }
        if (productId === 383039909737475) {
            entity.gainItem('suitG', false)

        }
        if (productId === 383034096431871) {
            entity.gainItem('hatG', false)

        }
        if (productId === 383040220115936) {
            entity.gainItem('mboxA', false)

        }
        if (productId === 383046033421444) {
            entity.gainItem('boss1Call', false)

        }
    }
});


world.querySelectorAll('#抽奖机').forEach(async (e) => {

    e.enableInteract = true;
    e.interactRadius = 5;
    e.interactHint = `神装打造机`;
    e.onInteract(async ({ entity }) => {
        let ll = 'jtNeed1'
        let need = entity.data[ll];
        let result = await dialog(entity, '神装打造机', `
        大奖奖池：皇家医生服、皇家医生帽、医疗手炮...
        （第6次必中深海自选奖）你有${entity.data.bk}个智能芯片
        中头奖之后继续从需要6智能芯片开始
        `, [`是（需要${need}智能芯片）`, `否`]);
        if (!result || result == null || result.index == 1) {
            return entity.player.directMessage(`下次一定记得再来抽哦～`)
        };
        if (entity.data.bk < need) {
            return entity.player.directMessage(`你的智能芯片不够`);
        } else {
            entity.data.bk -= need;
            if (entity.data[ll] == 0) {
                entity.data[ll] = 3;
            } else {
                entity.data[ll] *= 2;
            }
        }
        let r = Math.random();
        if (need >= 48) {
            r = 1;
        }
        if (r < 0.6) {
            entity.gainCoin(20000);
            dialog(entity, '打造', `没有打造成功，获得了20000夸克，再接再厉吧～`)
        } else if (r < 0.9) {
            if (!entity.bossReward) {
                entity.bossReward = 0;
            }
            entity.bossReward += 2;
            entity.gainExp(8000);
            dialog(entity, '打造', `运气还行，打造意外收获了2次boss购物机会+8000经验～，再接再厉吧～`)

        } else {
            entity.data[ll] = 6;
            let l = ['mboxA','suitA','hatA'];
            let names = [];
            for (let i of l) {
                names.push(ItemDict[i].name)
            }
            while (1) {
                let result1 = await dialog(entity, '打造成功！', `请选择一项作为你的奖励`, names);
                if (!result1 || result1 == null) {
                    continue;
                };
                entity.data.item.push(l[result1.index]);
                dialog(entity, '奖励已发放', `看看你的背包吧～`);
                break;
            }
        }
    })

})

const zh = world.querySelector('#金条置换');
zh.enableInteract = true; // 允许进行互动
zh.interactRadius = 3;   // 实体的互动范围
zh.interactHint = '互动置换金条' // 互动提示框显示实体的名称

// 玩家与实体进行交互时触发
zh.onInteract(async ({ entity }) => {
    const result = await entity.player.dialog({
        type: GameDialogType.TEXT,   // 对话框的类型，TEXT是文本框。            // 相机镜头对准NPC
        title: '提示',
        content: `你将${entity.data.jt}金条置换成了${entity.data.jt}智能芯片`,

    });
    entity.data.bk += entity.data.jt
    entity.data.jt = 0
    console.log(entity.data.bk)
    console.log(entity.data.jt)

});

world.querySelectorAll('#抽奖机1').forEach(async (e) => {
    e.enableInteract = true;
    e.interactRadius = 5;
    e.interactHint = `宝藏抽奖机`;
    e.onInteract(async ({ entity }) => {
        let aa = 'kkNeed1'
        let need1 = entity.data[aa];
        let result = await dialog(entity, '宝藏抽奖机', `
        大奖奖池：战术背包lv1。使用夸克抽取...
        （第5次必中神装自选奖）你有${entity.data.coin}个夸克
        中头奖之后继续从需要1250夸克开始
        `, [`是（需要${need1}夸克）`, `否`]);
        if (!result || result == null || result.index == 1) {
            return entity.player.directMessage(`下次一定记得再来抽哦～`)
        };
        if (entity.data.coin < need1) {
            return entity.player.directMessage(`你夸克不够`);
        } else {
            entity.data.coin -= need1;
            if (entity.data[aa] == 0) {
                entity.data[aa] = 1250;
            } else {
                entity.data[aa] *= 2;
            }
        }
        let r1 = Math.random();
        if (need1 >= 40000) {
            r1 = 1;
        }
        if (r1 < 0.6) {
            dialog(entity, '抽奖', `没有中奖，再接再厉吧～`)
        } else if (r1 < 0.9) {
            if (!entity.bossReward) {
                entity.bossReward = 0;
            }
            entity.bossReward += 1;
            entity.gainExp(5000);
            dialog(entity, '抽奖', `手气还行，抽到了1次boss购物机会+5000经验～，再接再厉吧～`)

        } else {
            entity.data[aa] = 1250;
            let a = ['bag++'];
            let names = [];
            for (let i of a) {
                names.push(ItemDict[i].name)
            }
            while (1) {
                let result1 = await dialog(entity, '头奖！', `请选择一项作为你的奖励`, names);
                if (!result1 || result1 == null) {
                    continue;
                };
                entity.data.item.push(a[result1.index]);
                dialog(entity, '奖励已发放', `看看你的背包吧～`);
                break;
            }
        }
    })

})


